<?php
/**
 * Description:
 *
 * @param type $var Description:
 * @return type Description:
 *
 */
class Captcha_controller extends MY_Controller {
    public function __construct() {
        parent::MY_Controller();
        $this->load->helper('form');

    }
    
    /*function generate() {
        $this->load->library('captcha');
        $this->load->library('validation');
        // $rules['user']    = "required";
        $rules['captcha']    = "required|callback_captcha_check";
        $this->validation->set_rules($rules);

        // $fields['user']    = 'Username';
        $fields['captcha']    = 'Captcha';
        $this->validation->set_fields($fields);
        if ($this->validation->run() == FALSE) {
            $expiration = time()-300; // Two hour limit
            $this->db->query("DELETE FROM lr_captcha WHERE captcha_time < ".$expiration);
            $vals = array(
                //'word'         => 'Random word',
                'img_path'     => './public/uploads/captcha/',
                'img_url'     => base_url().'public/uploads/captcha/',
                //  'font_path'     => './public/fonts/texb.ttf',
                'img_width'     => '100',
                'img_height' => '30',
                'expiration' => '3600'
            );

            $cap = $this->captcha->create_captcha($vals);
            //
            $dati['image']= $cap['image'];
            //mette nel db
            $data = array(
                'captcha_id'    => '',
                'captcha_time'    => $cap['time'],
                'ip_address'    => $this->input->ip_address(),
                'word'            => $cap['word']
            );
            $this->load->library('firephp');
            $this->firephp->log($dati['image']);

            $query = $this->db->insert_string('captcha', $data);
            $this->db->query($query);
            $this->load->view('captcha',$dati);
        }
        else {
            echo "OKi";
        }
    }

    function captcha_check() {
        // Then see if a captcha exists:
        $exp=time()-600;
        $sql = "SELECT COUNT(*) AS count FROM lr_captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
        $binds = array($this->input->post('captcha'), $this->input->ip_address(), $exp);
        
        $query = $this->db->query($sql, $binds);
        $row = $query->row();
        $this->firephp->log($row->count);
        if ($row->count == 0) {
          //  $this->validation->set_message('captcha_check', 'Code is not valid');
            return FALSE;
        }else {
            return TRUE;
        }

    }
*/
function generate(){
        $this->load->library('captcha');
        $this->load->library('validation');
        //$rules['user']    = "required";
        $rules['captcha']    = "required|callback_captcha_check";
        $this->validation->set_rules($rules);

    //    $fields['user']    = 'Username';
        $fields['captcha']    = 'codice';
        $this->validation->set_fields($fields);
        if ($this->validation->run() == FALSE)
        {
            $expiration = time()-300; // Two hour limit
            $this->db->query("DELETE FROM lr_captcha WHERE captcha_time < ".$expiration);
            $vals = array(
                        //'word'         => 'Random word',
                        'img_path'     => './public/uploads/',
                        'img_url'     => base_url().'/public/uploads/',
                       // 'font_path'     => './system/fonts/texb.ttf',
                        'img_width'     => '220',
                        'img_height' => '30',
                        'expiration' => '3600'
                    );

            $cap = $this->captcha->create_captcha($vals);
            //
            $dati['image']= $cap['image'];
            //mette nel db
            $data = array(
                        'captcha_id'    => '',
                        'captcha_time'    => $cap['time'],
                        'ip_address'    => $this->input->ip_address(),
                        'word'            => $cap['word']
                    );

            $query = $this->db->insert_string('captcha', $data);
            $this->db->query($query);
            $this->load->view('captcha',$dati);
        }else{
            echo "OK";
        }
    }

    function captcha_check()
    {
            // Then see if a captcha exists:
            $exp=time()-600;
            $sql = "SELECT COUNT(*) AS count FROM lr_captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
            $binds = array($this->input->post('captcha'), $this->input->ip_address(), $exp);
            $query = $this->db->query($sql, $binds);
            $row = $query->row();

            if ($row->count == 0)
            {
                $this->validation->set_message('_captcha_check', 'Codice di controllo non valido');
                return FALSE;
            }else{
                return TRUE;
            }

        }


}


